@tool

extends Node2D
class_name ColorBody

enum CollisionType
{
	NORMAL,
	INVERTED
}

enum Colors
{
	WHITE       = 17,
	RED         = 18,
	GREEN       = 19,
	BLUE        = 20,
	CYAN        = 21,
	MAGENTA     = 22,
	YELLOW      = 23,
}

static var ColorMap = {
	Colors.WHITE: Color.WHITE,
	Colors.RED: Color.RED,
	Colors.GREEN: Color.GREEN,
	Colors.BLUE: Color.BLUE,
	Colors.CYAN: Color.CYAN,
	Colors.MAGENTA: Color.MAGENTA,
	Colors.YELLOW: Color.YELLOW,
}
static func get_color(index : Colors) -> Color:
	return ColorMap[index]

@export var color : Colors = Colors.WHITE
@export var collision_type : CollisionType = CollisionType.NORMAL

const color_mask : int = 0b11111111111111110000000000000000

func _ready() -> void:
	if not Engine.is_editor_hint():
		_set_parent_collision()
	modulate = ColorBody.get_color(color)

func _set_parent_collision() -> void:
	var parent = get_parent()
	match collision_type:
		CollisionType.NORMAL:
			parent.set_collision_layer_value(color, true)
			parent.set_collision_mask_value(color, true)
		CollisionType.INVERTED:
			#It isn't so obvious but even if object is green, it will be on all
			# color layers except green
			parent.collision_mask = color_mask ^ parent.collision_mask
			parent.collision_layer = color_mask ^ parent.collision_layer
			parent.set_collision_layer_value(color, false)
			parent.set_collision_mask_value(color, false)
